<template>
  <!-- 使用自定义模态框替代 Bootstrap -->
  <div v-if="isModalOpen" class="custom-modal">
    <div class="modal-content">
      <span @click="closeModal">&times;</span>
      <h5>注册账号</h5>
      <div class="modal-body">
        <div>账号：<input v-model="username" /></div>
        <div>密码：<input v-model="password" type="password" /></div>
        <div>重复密码：<input v-model="password2" type="password" /></div>
      </div>
      <div class="modal-footer">
        <button @click="closeModal">取消</button>
        <button @click="register">注册账号</button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import axios from 'axios'
import { API_BASE_URL } from '@/utils/constants'

const isModalOpen = ref(false)
const username = ref('')
const password = ref('')
const password2 = ref('')

const register = async () => {
  if (password.value !== password2.value) {
    alert('两次密码不一致')
    return
  }

  try {
    await axios.post(`${API_BASE_URL}/admins/add`, {
      adminUsername: username.value,
      adminPasswordHash: password.value
    })
    alert('注册成功')
    closeModal()
  } catch (error) {
    alert(error.response?.data?.message || '注册失败')
  }
}

// 监听全局事件
const closeModal = () => isModalOpen.value = false

onMounted(() => {
  window.addEventListener('open-register-modal', () => {
    isModalOpen.value = true
  })
})
</script>

<style scoped>
/* 模态框容器 */
.custom-modal {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10000;
  backdrop-filter: blur(4px); /* 背景模糊 */
  transition: opacity 0.3s ease;
}

/* 模态框内容 */
.modal-content {
  background: white;
  border-radius: 12px;
  max-width: 500px;
  width: 90%;
  padding: 30px;
  position: relative;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
  animation: fadeIn 0.3s ease-in-out;
}

/* 关闭按钮 */
.modal-content span {
  position: absolute;
  right: 12px;
  top: 8px;
  font-size: 24px;
  color: #999;
  cursor: pointer;
  transition: color 0.2s;
}

.modal-content span:hover {
  color: #333;
}

/* 标题 */
h5 {
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 20px;
  color: #333;
  text-align: center;
}

/* 表单区域 */
.modal-body {
  display: flex;
  flex-direction: column;
  gap: 15px;
  margin-bottom: 20px;
}

/* 输入项容器 */
.modal-body div {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* 输入框 */
.modal-body input {
  width: 95%;
  padding: 10px 15px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 16px;
  transition: all 0.2s;
}

.modal-body input:focus {
  border-color: #409EFF;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
  outline: none;
}

/* 按钮容器 */
.modal-footer {
  display: flex;
  justify-content: space-between;
  gap: 15px;
}

/* 按钮基础样式 */
.modal-footer button {
  flex: 1;
  padding: 10px 15px;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  cursor: pointer;
  font-weight: 500;
  transition: all 0.2s;
}

/* 取消按钮 */
.modal-footer button:nth-child(1) {
  background-color: #f1f1f1;
  color: #666;
}

.modal-footer button:nth-child(1):hover {
  background-color: #e0e0e0;
}

/* 注册按钮 */
.modal-footer button:nth-child(2) {
  background-color: #409EFF;
  color: white;
}

.modal-footer button:nth-child(2):hover {
  background-color: #337ecc;
}

/* 响应式适配 */
@media (max-width: 600px) {
  .modal-content {
    padding: 20px;
    border-radius: 8px;
  }

  .modal-body input,
  .modal-footer button {
    font-size: 15px;
  }

  .modal-footer {
    flex-direction: column;
    gap: 10px;
  }
}

/* 动画定义 */
@keyframes fadeIn {
  from { opacity: 0; transform: scale(0.95); }
  to { opacity: 1; transform: scale(1); }
}
</style>